<template>
  <el-dialog
    v-model="visible"
    title="修改支付系统配置"
    width="500px"
    @close="resetForm"
  >
    <el-form ref="userFormRef" :model="form" :rules="rules">
      <el-form-item label="订单过期时间（分钟）" prop="orderExpireMinutes">
        <el-input-number
          v-model="form.orderExpireMinutes"
          :min="1"
          :step="1"
          step-strictly
        />
      </el-form-item>
      <el-form-item label="每小时最大订单数" prop="maxOrderPerHour">
        <el-input-number
          v-model="form.maxOrderPerHour"
          :min="1"
          :step="1"
          step-strictly
        />
      </el-form-item>
      <el-form-item label="最小支付金额" prop="minAmount">
        <el-input-number v-model="form.minAmount" :min="0" />
      </el-form-item>
      <el-form-item label="最大支付金额" prop="maxAmount">
        <el-input-number v-model="form.maxAmount" :min="0" />
      </el-form-item>
      <el-form-item label="积分汇率" prop="pointsRate">
        <el-input-number v-model="form.pointsRate" :min="0" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button :loading="submitLoading" type="primary" @click="submitForm">
        确认
      </el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { setAdminPayment } from "@/api/adminDynamicApi";
import { message } from "@/utils/message";

interface UserForm {
  orderExpireMinutes: number;
  maxOrderPerHour: number;
  minAmount: number;
  maxAmount: number;
  pointsRate: number;
}
const visible = ref(false);

const form = reactive<UserForm>({
  orderExpireMinutes: 0,
  maxOrderPerHour: 0,
  minAmount: 0,
  maxAmount: 0,
  pointsRate: 0
});

const emits = defineEmits(["refresh"]);

const rules: FormRules = {
  orderExpireMinutes: [
    {
      required: true,
      message: "请输入订单过期时间",
      trigger: "blur"
    }
  ],
  maxOrderPerHour: [
    {
      required: true,
      message: "请输入每小时最大订单数",
      trigger: "blur"
    }
  ],
  minAmount: [
    {
      required: true,
      message: "请输入最小支付金额",
      trigger: "blur"
    }
  ],
  maxAmount: [
    {
      required: true,
      message: "请输入最大支付金额",
      trigger: "blur"
    }
  ],
  pointsRate: [
    {
      required: true,
      message: "请输入积分汇率",
      trigger: "blur"
    }
  ]
};

const userFormRef = ref<FormInstance>();
const submitLoading = ref(false);
const submitForm = () => {
  userFormRef.value?.validate(async valid => {
    if (valid) {
      try {
        submitLoading.value = true;
        await setAdminPayment(form);
        message("更新成功", { type: "success" });
        visible.value = false;
        emits("refresh");
      } catch (error) {
        console.error(error);
      } finally {
        submitLoading.value = false;
      }
    }
  });
};

const resetForm = () => {
  userFormRef.value?.resetFields();
  userFormRef.value?.clearValidate();
};
const open = ({
  orderExpireMinutes,
  maxOrderPerHour,
  minPaymentAmount,
  maxPaymentAmount,
  pointsRate
}) => {
  form.orderExpireMinutes = orderExpireMinutes;
  form.maxOrderPerHour = maxOrderPerHour;
  form.minAmount = minPaymentAmount;
  form.maxAmount = maxPaymentAmount;
  form.pointsRate = pointsRate;
  visible.value = true;
};

defineExpose({ open });
</script>
